using System;
using System.Collections.Generic;
using System.Text;
using GuavaData;
using System.Windows.Forms;
using System.ComponentModel;

namespace GuavaLib
{
    public class GMonthCalendar : MonthCalendar, Guava
    {
        string gname;
        System.Windows.Forms.Label lab;
#if (SPARCE)
        private ContextMenu popUpMenu;
        private MenuItem markItem;
#endif
        public System.Windows.Forms.Label Label
        {
            get { return lab; }
            set { lab = value; }
        }

        public GMonthCalendar()
            : base()
        {
#if (SPARCE)
            this.markItem = new MenuItem("Mark", new EventHandler(markSelection));
            CreateMenu();
            this.ContextMenu = popUpMenu;
#endif
            }
        
#if (SPARCE)
        public void CreateMenu()
        {
            popUpMenu = new ContextMenu();
            popUpMenu.MenuItems.Add(markItem);
            // checkQueryMethod = new MenuItem("Query Method", new EventHandler(popup));
            // popUpMenu.MenuItems.Add(checkReturn);
            // popUpMenu.MenuItems.Add(checkQueryMethod);
            // checkQueryMethod.MenuItems.AddRange(regEx);
        }
        
        private void markSelection(object sender, EventArgs e)
        {
            // Verify that 'mark' was selected
            MenuItem miClicked = sender as MenuItem;
            if (miClicked.Text != "Mark") throw new Exception("Did not understand menu selection " + miClicked.Text);

            // Grab the current GSeed
            Control p = this;
            Control q = p.Parent;
            List<string> ls = new List<string>();
            ls.Add(this.GName);

            // Follow parents up the chain to the root form
            while (!(q is GuavaForm))
            {
                if (!(q is Guava))
                {
                    Guava r = q.Parent as Guava;
                    if (r == null) throw new Exception("Found two non-guava objects nested together in the same tree for marking.");
                    bool bfound = false;
                    foreach (Guava g in r.GComponents)
                    {
                        if (g.GComponents.Contains(p as Guava))
                        {
                            ls.Insert(0, g.GName);
                            bfound = true;
                            break;
                        }
                    }
                    if (!bfound) throw new Exception("Could not match a guava object for creating a seed for marking.");
                }
                else ls.Insert(0, (q as Guava).GName);
                p = p.Parent;
                q = q.Parent;
            }

            // Grab current seed and seed frame
            GSeed gs = (q as GuavaForm).GS;
            GSeedFrame gsf = gs.Peek();
            gsf.controls = ls;

            // Send mark to SPARCE
            SPARCEClipboard.ClipboardClass cc = new SPARCEClipboard.ClipboardClass();
            cc.CopyFodder("GuavaLauncher.GuavaLauncher", "Guava", "GuavaForms", gs.root,
                AppData.assemblyPath, "GuavaSeed", gs.ToString(), gs.formPath + ":" + this.GText,
                this.SelectionStart.Date.ToShortDateString() + " - " + this.SelectionEnd.Date.ToShortDateString(), Environment.MachineName,
                Environment.UserName, DateTime.Now.ToString(), false);
        }
#endif
        #region Guava Members

        [Browsable(false)]
        public LaunchType LType
        {
            get { return LaunchType.NA; }
        }

        [Browsable(false)]
        public ControlType CType
        {
            get { return ControlType.Container; }
        }

        [Browsable(false)]
        public string GText
        {
            get
            {
                if (this.lab == null) return null;
                return this.lab.Text;
            }
        }

        public string GName
        {
            get
            {
                if (gname == null) return this.Name;
                return gname;
            }
            set
            {
                gname = value;
            }
        }

        [Browsable(false)]
        public System.Drawing.Point GLocation
        {
            get { return new System.Drawing.Point(); }
        }

        [Browsable(false)]
        public System.Drawing.Size GSize
        {
            get { return new System.Drawing.Size(); }
        }

        [Browsable(false)]
        public List<Guava> GComponents
        {
            get
            {
                List<Guava> lg = new List<Guava>();
                lg.Add(new GEmpty(this.GName + "~SelectionStart", new GuavaData.Domain.DateTime()));
                lg.Add(new GEmpty(this.GName + "~SelectionEnd", new GuavaData.Domain.DateTime()));
                return lg;
            }
        }

        public void GBind(string table, int i)
        {
            this.DataBindings.Clear();
            System.Data.DataRow dr = AppData.ds.Tables[table].Rows[i];
            string ss = this.GName + "~SelectionStart";
            string se = this.GName + "~SelectionEnd";
            if (dr[ss] is DBNull) dr[ss] = DateTime.Now;
            if (dr[se] is DBNull) dr[se] = DateTime.Now;
            this.DataBindings.Add("SelectionStart", AppData.ds.Tables[table], ss);
            this.DataBindings.Add("SelectionEnd", AppData.ds.Tables[table], se);
        }

        [Browsable(false)]
        public string GDefault
        {
            get { return null; }
        }

        [Browsable(false)]
        public string GToolTip
        {
            get
            {
                Control p = this.TopLevelControl;
                if (!(p is Guava)) return null;
                ToolTip t = (p as GuavaForm).GToolTipProvider;
                if (t == null) return null;

                // Fetch the tooltip and return it
                return t.GetToolTip(this);
            }
        }

        [Browsable(false)]
        public GuavaData.Domain.Domain GDomain
        {
            get { return null; }
        }

        public Guava this[string s]
        {
            get
            {
                foreach (Guava g in this.GComponents) if (g.GName == s) return g;
                return null;
            }
        }

        #endregion
    }
}
